/*
 * Copyright 2004 original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jbfilter.impl;

import org.jbfilter.core.CleaningStrategy;
import org.jbfilter.core.FilterComponent;
import org.jbfilter.core.JbfilterComparatorKeyword;
import org.jbfilter.core.fcomps.logic.LogicFilterComponent;
import org.jbfilter.core.fcomps.single.CompareFilterComponent;
import org.jbfilter.core.fcomps.single.ContainsStringFilterComponent;
import org.jbfilter.core.fcomps.single.SingleFilterComponent;
import org.jbfilter.core.fcomps.single.StringFilterComponent;

/**
 * Here we centralize all the default {@link CleaningStrategy}ies.
 * @author Marcus Adrian
 */
class _DefaultCleaningStrategies {

	public static <E> CleaningStrategy getFilterComponentCleaningStrategy(final FilterComponent<E> pFcomp) {
		return new CleaningStrategy() {
			@Override
			public void clean() {
				cleanFilterComponent(pFcomp);
			}
		};
	}
	public static <E, T> CleaningStrategy getSingleFilterComponentCleaningStrategy(final SingleFilterComponent<E, T> sfComp) {
		return new CleaningStrategy() {
			@Override
			public void clean() {
				cleanSingleFilterComponent(sfComp);
			}
		};
	}
	public static <E> CleaningStrategy getContainsStringFilterComponentCleaningStrategy(final ContainsStringFilterComponent<E> csfComp) {
		return new CleaningStrategy() {
			@Override
			public void clean() {
				cleanContainsStringFilterComponent(csfComp);
			}
		};
	}
	public static <E> CleaningStrategy getStringFilterComponentCleaningStrategy(final StringFilterComponent<E> sfComp) {
		return new CleaningStrategy() {
			@Override
			public void clean() {
				cleanStringFilterComponent(sfComp);
			}
		};
	}
	public static <E, T> CleaningStrategy getCompareFilterComponentCleaningStrategy(final CompareFilterComponent<E, T> dfComp) {
		return new CleaningStrategy() {
			@Override
			public void clean() {
				cleanCompareFilterComponent(dfComp);
			}
		};
	}
	public static <E> CleaningStrategy getLogicFilterComponentCleaningStrategy(final LogicFilterComponent<E> lfComp) {
		return new CleaningStrategy() {
			@Override
			public void clean() {
				cleanLogicFilterComponent(lfComp);
			}
		};
	}
	
	private static <E> void cleanFilterComponent(FilterComponent<E> fcomp) {
		fcomp.setInverted(false);
	}
	private static <E, T> void cleanSingleFilterComponent(SingleFilterComponent<E, T> sfComp) {
		cleanFilterComponent(sfComp);
		sfComp.setValue(sfComp.getValueCleanValue());
	}
	private static <E> void cleanContainsStringFilterComponent(ContainsStringFilterComponent<E> csfComp) {
		cleanSingleFilterComponent(csfComp);
		Boolean caseSensitiveCleanValue = csfComp.getCaseSensitiveCleanValue();
		if (caseSensitiveCleanValue != null) {
			csfComp.setCaseSensitive(caseSensitiveCleanValue.booleanValue());	
		}
	}
	private static <E> void cleanStringFilterComponent(StringFilterComponent<E> sfComp) {
		cleanContainsStringFilterComponent(sfComp);
		Boolean regexCleanValue = sfComp.getRegexCleanValue();
		if (regexCleanValue != null) {
			sfComp.setRegex(regexCleanValue.booleanValue());
		}
	}
	private static <E, T> void cleanCompareFilterComponent(CompareFilterComponent<E, T> cfComp) {
		cleanSingleFilterComponent(cfComp);
		JbfilterComparatorKeyword comparatorKeywordCleanValue = cfComp.getComparatorKeywordCleanValue();
		if (comparatorKeywordCleanValue != null) {
			cfComp.setComparatorKeyword(comparatorKeywordCleanValue);
		}
	}
	private static <E> void cleanLogicFilterComponent(LogicFilterComponent<E> lfComp) {
		for (FilterComponent<E> fc : lfComp.getFilterComponents()) {
			fc.clean();
		}
	}
}
